$(document).ready( function() {
	
	//获取项目名
	var contextPath = document.location.pathname; 
	var index =contextPath.substr(1).indexOf("/"); 
	contextPath = contextPath.substr(0,index+1); 

	var isReading = false; // 是否处于监视用户输入状态
	var commArr = new Array()// 定义一个数组，用来存储评论实体
	var addPageCount = 1;// 初始化为1
	var len = 0;// 标记数组的长度，删除时-1
	
	/**
	 * 清空table（除了table的首行）
	 * */
	function clearItemTable() {
		$("#itemsTable tr:not(:first)").remove();
	}
	/**
	 * 清空数组
	 */
	function clearArrAll(){
		if(commArr.length>0){
			commArr.splice(0,commArr.length);
		}
	}
	
	/**
	 * 回复按钮事件
	 */
	$("#commList").delegate('#viewCommentDir', 'click', function() {
		//清空textarea的值
		$("#reContent").each(function() {
			$(this).prop("value", null);
		});
		clearArrAll();//清空数组
		clearItemTable()//清空table
		var replyId =$(this).attr("objId");
		$("#replyId").val(replyId);
		
		var queryUrl = contextPath+"/manage/comments/replys/queryReply.do";
		if (replyId != "") {
			clearArrAll();//清除数组
			$.ajax({
				url : queryUrl,
				type : "POST",
				dataType : 'json',
				data : {
					id : replyId,
				},
				cache : false,
				success : function(data) {
					len = data.resultList.length;
					for (i = 0; i < len; i++) {
						var result = data.resultList[i];
						commArr.push(result);
					}
					getQuery(len);//分页函数
				},
				error : function(html) {
					alert("出错了");
					return;
				}
			});
		} else {
			/**
			 * 添加的内分页
			 */
			$('#itemPagination').jqPaginator({
				totalPages : addPageCount,
				visiblePages : 10,
				currentPage : 1,
				prev : '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
				page : '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
				next : '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
				onPageChange : function(num,type) {
					clearItemTable();
					getQuery(len);//分页函数
				}
			});
		}
		
		$("#replyDir").modal('show');
	});
	
	
	
	/**
	 * 
	 * 添加回复
	 */
	$("#searchInfor").delegate('#addItem', 'click', function() {
		var replyId = $("#replyId").val();
		var reContent = $("#reContent").val();
		var replysUrl = contextPath+"/manage/comments/replys/loadReplys.do?id=" + replyId;
		$.ajax({
			url : replysUrl,
			dataType : "json",
			success : function(data) {
				if (data != null) {
					data.content = reContent;
					pushReplys(data);//这个方法用来内分页
					return;
				}
			},
			error : function(data) {
				alert("加载数据出错");
				return;
			}
		});
	});
	
	
	/**
	 * 存储回复
	 */
	function pushReplys(data) {
		commArr.push(data);
		len=commArr.length;
		getQuery(len);// 分页
	}
	
	
	/**
	 * 分页函数
	 */
	function getQuery(len) {
		/**
		 * 添加的内分页
		 */
		if (len <= 5) {
			addPageCount = 1;
		} else {
			if (len % 5 == 0) {
				addPageCount = len / 5;
			} else {
				addPageCount = Math.floor((len / 5)) + 1;
			}
		}
		$('#itemPagination').jqPaginator({
			totalPages : addPageCount,
			visiblePages : 10,//可以看到的页数
			currentPage : 1,
			prev : '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
			page : '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
			next : '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
			onPageChange : function(num, type) {
				clearItemTable();
				var itemTable = $("#itemsTable");
				var start = num * 5 - 5;
				var end = num * 5;
				var idx = 0;
				for (i = start; i < commArr.length; i++) {
					if(idx<5){
						lookItemContent(itemTable,commArr[i],i);
						idx++;
					}
				}
			}
		});
		return true;
	}
	
	/**修改*/
	/*多余的框要给其赋值；否则在添加产品所有除了产品本身的字段外，其他的都没有值*/
	function lookItemContent(itemTable, data,rowNum) {
		var commentsId= $("#replyId").val();
		var rowContent = "<tr id="
		+ data.hrtMemberId
		+ ">" 
		//会员名称
		+ "<td style='text-align: center;'>" 
		+ "<input id='hrtMemberName_"
		+ data.id + "' type='text' name='replysVO["
		+ rowNum
		+ "].hrtMemberName' itemName='hrtMemberName' value='"
		+ data.hrtMemberName + "' readonly='readonly' />" ;
		
		if(data.id==null||data.id=='undefined'){
			rowContent = rowContent
			//回复的id
			+ "<input type='hidden'  id='id_"
			+ data.id + "'  name='replysVO[" + rowNum
			+ "].id' itemName='id' />" ;
		}
		else{
			rowContent = rowContent
			//回复的id hidden
			+ "<input type='hidden'  id='id_"
			+ data.id + "'  name='replysVO[" + rowNum
			+ "].id' itemName='id' value='"+data.id+"'/>" ;
		}
		rowContent = rowContent 
		//会员的id
		+"<input type='hidden' id='hrtMemberId_' name='replysVO[" + rowNum
		+ "].hrtMemberId' itemName='hrtMemberId' value='"+ data.hrtMemberId +"'/>"
		+ "</td>"
		
		//礼品规格的名称
		+ "<td style='text-align: center;'>" 
		+ "<input id='gdeName_"
		+ data.id + "' type='text' name='replysVO["
		+ rowNum
		+ "].gdeName' itemName='gdeName' value='"
		+ data.gdeName + "' readonly='readonly' />" 
		//礼品规格的id
		+"<input type='hidden' id='gdeId_' name='replysVO[" + rowNum
		+ "].gdeId' itemName='gdeId' value='"+ data.gdeId +"'/>"
		//评论的id
		+"<input type='hidden' id='commentsId_' name='replysVO[" + rowNum
		+ "].commentsId' itemName='commentsId' value='"+ commentsId +"'/>"
		+ "</td>"
		
		
		if(data.content == null || data.content == 'undefined'){
			//回复内容
			rowContent = rowContent
			+ "<td style='text-align: center;'>" 
			+ "<input id='content_"
			+ data.id + "' type='text' name='replysVO["
			+ rowNum + "].content' rowId='" + rowNum
			+ "' itemName='content' />" 
			+ "</td>";
		}else{
			//回复内容
			rowContent = rowContent
			+ "<td style='text-align: center;'>" 
			+ "<input id='content_"
			+ data.id + "' type='text' name='replysVO["
			+ rowNum + "].content' rowId='" + rowNum
			+ "' itemName='content' value='"+data.content+"'/>" 
			+ "</td>";
		}
		
		if(data.note == null || data.note == 'undefined'){
			//备注
			rowContent = rowContent
			+ "<td style='text-align: center;'>" 
			+ "<input id='note_"
			+ data.id + "' type='text' name='replysVO["
			+ rowNum + "].note' rowId='" + rowNum
			+ "' itemName='note' />" 
			+ "</td>";
		}else{
			//备注
			rowContent = rowContent
			+ "<td style='text-align: center;'>" 
			+ "<input id='note_"
			+ data.id + "' type='text' name='replysVO["
			+ rowNum + "].note' rowId='" + rowNum
			+ "' itemName='note' value='"+data.note+"'/>" 
			+ "</td>";
		}
		
		
		//操作
		rowContent = rowContent
		+ "<td style='text-align: center;'>" 
		+ "<button name='delItem' rowId='"+rowNum+"' delId='"
		+ data.id+ "' type='button'>删除</button>" 
		+ "</td>";
	
		rowContent = rowContent + "</tr>";
		itemTable.append(rowContent);
		rowNum++;
	}
	
	/**
	 * 删除添加的记录
	 */
	$("#itemsTable").delegate("button[name='delItem']", 'click', function() {
		if (confirm("是否要删除该记录?") == true) {
			var trId = $(this).attr("rowId");
			var deltrId = $(this).attr("delId");
			var itemId = $("#id_" + deltrId).val();
			var id = $(this).parent().parent().attr("id");
			if (itemId == null || itemId == ""|| itemId == "undefined") {
				commArr.splice(trId,1);
				$(this).parent().parent().remove();
				len = commArr.length;
				getQuery(len);
			} else {
				var delUrl = contextPath+"/manage/comments/replys/delItemById.do";
				$.ajax({
					url : delUrl,
					dataType : 'text',
					data : {
						id : itemId
					},
					cache : false,
					success : function(data) {
						commArr.splice(trId,1);
						$(this).parent().parent().remove();
						len = commArr.length;
						getQuery(len);
						return;
					},
					error : function(html) {
						alert("delete Data failly");
						return;
					}
				});
			}
		}
	});
	
	/**
	 * 保存与修改数据
	 */
	$("#optBtnGR").delegate('#saveBtnGR', 'click', function() {
		$("#reply").attr("method", "post");
		$("#reply").attr("action", contextPath+"/manage/comments/replys/saveReply.do");
		clearItemTable();//清空table
		var itemTable = $("#itemsTable");
		for(i=0;i<commArr.length;i++){
			lookItemContent(itemTable,commArr[i],i);
		}
		$("#reply").submit();
	});
	
	
	/**修改之后赋值*/
	/*赋值备注content*/
	$("#itemsTable") .delegate("input[itemName='content']", 'blur', function() {
		var content = $(this).val();
		var id = $(this).parent().parent().attr("id");
		var trId = $(this).attr("rowId");
		$("#content" + id).val(content);
		var ele = commArr[trId];
		ele.content = content;
		commArr.splice(trId, 1, ele);
	});
	/*赋值备注note*/
	$("#itemsTable") .delegate("input[itemName='note']", 'blur', function() {
		var note = $(this).val();
		var id = $(this).parent().parent().attr("id");
		var trId = $(this).attr("rowId");
		$("#note" + id).val(note);
		var ele = commArr[trId];
		ele.note = note;
		commArr.splice(trId, 1, ele);
	});
	
	
	
	
});

	

/**
 * 刷新页面
 * */
function refresh() {
	window.location.reload();
}
